package com.tyl.service.user;

import com.tyl.dao.BaseDao;
import com.tyl.dao.user.UserDao;
import com.tyl.dao.user.UserDaoImpl;
import com.tyl.pojo.User;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;


public class UserServiceImpl implements UserService {
    //业务层会调用dao层，所以引入dao
    private UserDao userDao;
    public UserServiceImpl(){
        userDao=new UserDaoImpl();
    }

    /**
     * 登录判断
     * **/
    public User login(String userCode, String userPassword) {
        Connection connection=null;
        User user=null;
        try {
            connection = BaseDao.getConnection();
            // 通过业务层调用dao层的方法，
            user = userDao.getLoginUser(connection, userCode,userPassword);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //关闭资源
            BaseDao.closeResource(connection,null,null);
        }
        return user;
    }

    /**
     * 根据用户id修改密码
     * **/
    public boolean updatePwd(int id, String password) {
        Connection connection=null;
        boolean flag=false;
        //修改密码
        try {
            connection = BaseDao.getConnection();
            if (userDao.updatePwd(connection,id,password)>0){
                flag =true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    /**
     * 根据用户名或者用户角色查询用户总数
     * **/
    public int getUserCount(String username, int userRole) {
        Connection connection=null;
        int count=0;
        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection, username, userRole);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return count;
    }
    /**
     * 根据条件查询用户列表
     * **/
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection=null;
        List<User> userList=null;
        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection, queryUserName, queryUserRole, currentPageNo, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return userList;
    }

    public boolean addUser(User user) {
        // TODO Auto-generated method stub

        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务管理
            int updateRows = userDao.addUser(connection, user);
            connection.commit();
            if (updateRows > 0) {
                flag = true;
                System.out.println("add success!");
            } else {
                System.out.println("add failed!");
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            try {
                System.out.println("rollback==================");
                connection.rollback();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } finally {
            //在service层进行connection连接的关闭
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }


    //通过用户编码查询用户
    public User getUserCode(String userCode) {
        Connection connection=null;
        User user=null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getUserCode(connection, userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return user;
    }

    public User getUserId(String userId) throws SQLException {
        Connection connection=null;
        User user=null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getUserId(connection, userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return user;
    }
    public int deleteByUserId(Integer userId) throws SQLException {
        Connection connection=null;
        connection= BaseDao.getConnection();
        int flag=0;
        if(connection!=null){
            flag= userDao.deleteByUserId(connection, userId);
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }
    //修改用户
    public int updateUser(User user) {
        Connection connection=null;
        connection = BaseDao.getConnection();
        int flag=0;
        if(connection!=null){
            flag= userDao.updateUser(connection,user);
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    @Test
    public void tese01() throws SQLException {
        UserService userService=new UserServiceImpl();
    }
}
